import { Table as AntTable, TableProps } from "antd";
import { useColumns } from "./useColumns";
import { useAddAndDel } from "./hooks";

export interface ITableProps extends TableProps<any> {
    bordered?: boolean;
    isFlex?: boolean;
    isAdd?: boolean;
    isDel?: boolean;
    defaultRowKey?: string;
    addCallback?: (record: any, info: { index: number }) => any;
    delCallBack?: (record: any, info: { index: number }) => any;
    onTableChange?: (
        dataSource: any,
        preDataSource: any,
        other?: {
            currentIndex?: number; // 当前操作的行
            dragIndex?: number; // 拖拽行
            hoverIndex?: number; // 放置行
            type?:
                | "edit"
                | "move"
                | "add"
                | "del"
                | "delAll"
                | "fillDown"
                | "sort"
                | "custom"; // 通过什么动作修改了表格数据
            field?: string; // 当前修改的字段
            [prop: string]: any;
        }
    ) => void;
}
const Table: React.FC<ITableProps> = (props) => {
    const {
        columns,
        isAdd,
        isDel,
        dataSource,
        defaultRowKey,
        addCallback,
        delCallBack,
        onTableChange,
        ...resetProps
    } = props;

    const onAddAndDel = useAddAndDel(
        dataSource,
        onTableChange,
        defaultRowKey,
        addCallback,
        delCallBack
    );
    // 处理列配置信息 得到新的列配置信息
    const { newColumns } = useColumns(columns, {
        isAdd,
        isDel,
        onAddAndDel,
    });
    return (
        <AntTable
            {...resetProps}
            columns={newColumns}
            dataSource={dataSource}
        />
    );
};
export default Table;
